/*

    This file is part of GLua

    GLua Development Environment
    Copyright (C) 2007 VoiDeD

    GLua is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    GLua is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace GLua
{
    /// <summary>
    /// Simple update checker
    /// </summary>
    // another TODO class, was going to be implemented on GLua.net to check for updates
    // a server script would output a string such as "0.4.0, Beta, 1", and then compare against
    // this assembly's version
    public class UpdateChecker
    {
        /// <summary>
        /// Checks for updates
        /// </summary>
        /// <returns>Returns a version information object which can be used to determine if a new version is available</returns>
        public static VersionInformation CheckForUpdates()
        {
            HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( StringTable.UpdateCheckURL );
            request.UserAgent = StringTable.UserAgent;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                response = ( HttpWebResponse )request.GetResponse();
                reader = new StreamReader( response.GetResponseStream() );
            }
            catch
            {
                Util.ShowError( StringTable.UnableToCheckForUpdates );

                VersionInformation vi = new VersionInformation();
                vi.UpdateAvailable = false;
                vi.Error = true;
                vi.LastestVersion = VersionSerializer.SerializeThis();
                vi.ErrorString = StringTable.UnableToCheckForUpdates;

                return vi;
            }

            string str = reader.ReadLine();

            string thisVer = VersionSerializer.SerializeThis();
            string newVer = str;

            ComparableVersion thisVersion = new ComparableVersion( thisVer );
            ComparableVersion newVersion = new ComparableVersion( newVer );

            if ( newVersion > thisVersion )
            {
                VersionInformation vi = new VersionInformation();

                vi.UpdateAvailable = true;
                vi.LastestVersion = newVer;

                return vi;
            }
            else
            {
                VersionInformation vi = new VersionInformation();

                vi.UpdateAvailable = false;
                vi.LastestVersion = thisVer;

                return vi;
            }
        }
    }
}
